﻿// ***********************************************************************
// Project			: Micua
// Assembly         : Micua.DALFactory
// Author           : iceStone
// Created          : 2013-11-18 11:59
//
// Last Modified By : iceStone
// Last Modified On : 2014-01-05 17:20
// ***********************************************************************
// <copyright file="DbSession.cs" company="Wedn.Net">
//     Copyright (c) Wedn.Net. All rights reserved.
// </copyright>
// <summary></summary>
// ***********************************************************************
using System.Data.Entity;
using System.Runtime.Remoting.Messaging;
using Micua.DAL;
using Micua.IDAL;

namespace Micua.RepositoryFactory
{
    /// <summary>
    /// 数据库操作会话级对象
    /// </summary>
    public class DbSession : IDbSession
    {
        private DbSession() { }

        private DbContext _dbContext;
        private DbContext DbContext
        {
            //NB的单例模式，跨时代的产品！（线程内实例唯一）
			get
            {
                return _dbContext ?? (_dbContext = CallContext.GetData(DALStruct.CurrentDbContextKey) as DbContext);
            }
        }
        //private readonly string _path = Config.DALAssembly;
        //private readonly string _nspace = string.IsNullOrEmpty(Config.DALNamespace) ? Config.DALAssembly : Config.DALNamespace;
        
        #region Repositories
        private ICategoryRepository _category;
        /// <summary>
        /// CategoryRepository 实例
        /// </summary>
		/// <remarks>
		///  2013-11-18 19:06 Created By iceStone
		///  2014-01-05 17:20 Modified By iceStone
		/// </remarks>
		public ICategoryRepository CategoryRepository
        {
            get
			{
                return _category ?? (_category = new CategoryRepository());
				//var className = _nspace + ".CategoryRepository";
                //if (_category == null) AssemblyHelper.CreateInstance("DbSessionLockHelper", _path, className, ref _category);
                //return _category;
			}
        }

        private ICommentRepository _comment;
        /// <summary>
        /// CommentRepository 实例
        /// </summary>
		/// <remarks>
		///  2013-11-18 19:06 Created By iceStone
		///  2014-01-05 17:20 Modified By iceStone
		/// </remarks>
		public ICommentRepository CommentRepository
        {
            get
			{
                return _comment ?? (_comment = new CommentRepository());
				//var className = _nspace + ".CommentRepository";
                //if (_comment == null) AssemblyHelper.CreateInstance("DbSessionLockHelper", _path, className, ref _comment);
                //return _comment;
			}
        }

        private ICommentMetaRepository _commentmeta;
        /// <summary>
        /// CommentMetaRepository 实例
        /// </summary>
		/// <remarks>
		///  2013-11-18 19:06 Created By iceStone
		///  2014-01-05 17:20 Modified By iceStone
		/// </remarks>
		public ICommentMetaRepository CommentMetaRepository
        {
            get
			{
                return _commentmeta ?? (_commentmeta = new CommentMetaRepository());
				//var className = _nspace + ".CommentMetaRepository";
                //if (_commentmeta == null) AssemblyHelper.CreateInstance("DbSessionLockHelper", _path, className, ref _commentmeta);
                //return _commentmeta;
			}
        }

        private ILinkRepository _link;
        /// <summary>
        /// LinkRepository 实例
        /// </summary>
		/// <remarks>
		///  2013-11-18 19:06 Created By iceStone
		///  2014-01-05 17:20 Modified By iceStone
		/// </remarks>
		public ILinkRepository LinkRepository
        {
            get
			{
                return _link ?? (_link = new LinkRepository());
				//var className = _nspace + ".LinkRepository";
                //if (_link == null) AssemblyHelper.CreateInstance("DbSessionLockHelper", _path, className, ref _link);
                //return _link;
			}
        }

        private IOptionRepository _option;
        /// <summary>
        /// OptionRepository 实例
        /// </summary>
		/// <remarks>
		///  2013-11-18 19:06 Created By iceStone
		///  2014-01-05 17:20 Modified By iceStone
		/// </remarks>
		public IOptionRepository OptionRepository
        {
            get
			{
                return _option ?? (_option = new OptionRepository());
				//var className = _nspace + ".OptionRepository";
                //if (_option == null) AssemblyHelper.CreateInstance("DbSessionLockHelper", _path, className, ref _option);
                //return _option;
			}
        }

        private IPostRepository _post;
        /// <summary>
        /// PostRepository 实例
        /// </summary>
		/// <remarks>
		///  2013-11-18 19:06 Created By iceStone
		///  2014-01-05 17:20 Modified By iceStone
		/// </remarks>
		public IPostRepository PostRepository
        {
            get
			{
                return _post ?? (_post = new PostRepository());
				//var className = _nspace + ".PostRepository";
                //if (_post == null) AssemblyHelper.CreateInstance("DbSessionLockHelper", _path, className, ref _post);
                //return _post;
			}
        }

        private IPostMetaRepository _postmeta;
        /// <summary>
        /// PostMetaRepository 实例
        /// </summary>
		/// <remarks>
		///  2013-11-18 19:06 Created By iceStone
		///  2014-01-05 17:20 Modified By iceStone
		/// </remarks>
		public IPostMetaRepository PostMetaRepository
        {
            get
			{
                return _postmeta ?? (_postmeta = new PostMetaRepository());
				//var className = _nspace + ".PostMetaRepository";
                //if (_postmeta == null) AssemblyHelper.CreateInstance("DbSessionLockHelper", _path, className, ref _postmeta);
                //return _postmeta;
			}
        }

        private ITagRepository _tag;
        /// <summary>
        /// TagRepository 实例
        /// </summary>
		/// <remarks>
		///  2013-11-18 19:06 Created By iceStone
		///  2014-01-05 17:20 Modified By iceStone
		/// </remarks>
		public ITagRepository TagRepository
        {
            get
			{
                return _tag ?? (_tag = new TagRepository());
				//var className = _nspace + ".TagRepository";
                //if (_tag == null) AssemblyHelper.CreateInstance("DbSessionLockHelper", _path, className, ref _tag);
                //return _tag;
			}
        }

        private IUserRepository _user;
        /// <summary>
        /// UserRepository 实例
        /// </summary>
		/// <remarks>
		///  2013-11-18 19:06 Created By iceStone
		///  2014-01-05 17:20 Modified By iceStone
		/// </remarks>
		public IUserRepository UserRepository
        {
            get
			{
                return _user ?? (_user = new UserRepository());
				//var className = _nspace + ".UserRepository";
                //if (_user == null) AssemblyHelper.CreateInstance("DbSessionLockHelper", _path, className, ref _user);
                //return _user;
			}
        }

        private IUserMetaRepository _usermeta;
        /// <summary>
        /// UserMetaRepository 实例
        /// </summary>
		/// <remarks>
		///  2013-11-18 19:06 Created By iceStone
		///  2014-01-05 17:20 Modified By iceStone
		/// </remarks>
		public IUserMetaRepository UserMetaRepository
        {
            get
			{
                return _usermeta ?? (_usermeta = new UserMetaRepository());
				//var className = _nspace + ".UserMetaRepository";
                //if (_usermeta == null) AssemblyHelper.CreateInstance("DbSessionLockHelper", _path, className, ref _usermeta);
                //return _usermeta;
			}
        }

		#endregion

        /// <summary>
        /// 保存数据库的改变状态
        /// </summary>
        /// <returns>受影响行数</returns>
        /// <remarks>
        ///  2013-11-18 19:06 Created By iceStone
        ///  2013-12-21 20:16 Modified By iceStone
        /// </remarks>
        public int SaveChanges()
        {
            return DbContext != null ? DbContext.SaveChanges() : 0;
            //db = new MicuaModelContainer();
            //CallContext.SetData(MicuaStruct.CurrentDbContextKey, db);
            //return db.SaveChanges();
        }

        /// <summary>
        /// 释放数据库上下文
        /// </summary>
        /// <remarks>
        ///  2013-11-18 19:06 Created By iceStone
        ///  2013-12-21 20:16 Modified By iceStone
        /// </remarks>
        public void Dispose()
        {
            DbContext.Dispose();
        }
		
        /// <summary>
        /// 获取当前线程内的DbSession对象；
        /// </summary>
        /// <remarks>
        ///  2013-11-18 19:06 Created By iceStone
        ///  2013-12-21 20:16 Modified By iceStone
        /// </remarks>
        public static IDbSession Instance
        {
            get
            {
                //NB的单例模式，跨时代的产品！（线程内实例唯一）
                var dbSession = CallContext.GetData(DALStruct.CurrentDbSessionKey) as IDbSession;
                if (dbSession != null) return dbSession;
                dbSession = new DbSession();
                CallContext.SetData(DALStruct.CurrentDbSessionKey, dbSession);
                return dbSession;
            }
        }
    }
} 
